﻿using SupermaketManager.Model;
using SupermarketManager.Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SupermaketManager.DataAccess
{
    class CTThongKeAccess
    {
        dbConnection db = new dbConnection();
        public CTThongKeAccess()
        {

        }
        public DataTable layToanboDSCTTK()
        {
            return db.executeSelectProcedureQueryNonParameter("select_ctthongke");
        }
        public DataView timkiemMathongke(string maKK)
        {
            DataView dataView = layToanboDSCTTK().DefaultView;
            dataView.RowFilter = string.Format("maKK like '%{0}%'", maKK);
            return dataView;
        }
        public DataTable timPhieukktheoma(string maKK)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma = new SqlParameter("@maKK", SqlDbType.VarChar, 10);
            ma.Value = maKK;
            listParam[0] = ma;
            return db.executeSelectProcedureQuery("select_ctthongke_theoma", listParam);
        }
        public int themCTKiemke(CTThongKe kk)
        {

            if (timPhieukktheoma(kk.MaKK).Rows.Count > 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[5];
                SqlParameter maKK = new SqlParameter("@maKK", SqlDbType.VarChar, 10);
                SqlParameter maHH = new SqlParameter("@maHH", SqlDbType.VarChar, 10);
                maKK.Value = kk.MaKK;
                maHH.Value = kk.MaHH;
                listParam[0] = maKK;
                listParam[1] = maHH;
                listParam[2] = new SqlParameter("@soLuongNhap", kk.SoLuongNhap);
                listParam[3] = new SqlParameter("@soLuongBan", kk.SoLuongBan);
                listParam[4] = new SqlParameter("@soLuongTonKho", kk.SoLuongTonKho);
                db.executeInsertProcedureQuery("insert_ctthongke", listParam);
                return 1;
            }
        }
        public int suaCTKK(CTThongKe kk)
        {
            SqlParameter[] listParam = new SqlParameter[5];
            SqlParameter maKK = new SqlParameter("@maKK", SqlDbType.VarChar, 10);
            SqlParameter maHH = new SqlParameter("@maHH", SqlDbType.VarChar, 10);
            maKK.Value = kk.MaKK;
            maHH.Value = kk.MaHH;
            listParam[0] = maKK;
            listParam[1] = maHH;
            listParam[2] = new SqlParameter("@soLuongNhap", kk.SoLuongNhap);
            listParam[3] = new SqlParameter("@soLuongBan", kk.SoLuongBan);
            listParam[4] = new SqlParameter("@soLuongTonKho", kk.SoLuongTonKho);
            db.executeUpdateProcedureQuery("update_ctthongke", listParam);
            return 1;
        }
        public int xoaCTKK(CTThongKe kk)
        {

            if ((timkiemMathongke(kk.MaKK) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[1];
                listParam[0] = new SqlParameter("@maKK", kk.MaKK);
                db.executeDeleteProcedureQuery("delete_ctthongke", listParam);
                return 1;
            }
        }
    }
}
